package com.ruoyi.project.order.service.impl;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.order.domain.HisCareHistory;
import com.ruoyi.project.order.domain.HisCareOrder;
import com.ruoyi.project.order.domain.HisCareOrderItem;
import com.ruoyi.project.order.service.HisCareOrderItemService;
import com.ruoyi.project.order.service.HisCareOrderService;
import com.ruoyi.project.order.service.IHisCareHistoryService;
import com.ruoyi.project.order.service.IOrderNoChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderChargeServiceImpl implements IOrderNoChargeService {

    /** 注入其他 服务 */
    @Autowired
    private IHisCareHistoryService careHistoryService;

    @Autowired
    private HisCareOrderService orderService;

    @Autowired
    private HisCareOrderItemService orderItemService;
@Transactional
    @Override
    public AjaxResult selectNoChargeOrderItemAndCareHisTory(String regId) {
        Map<String,Object> res = new HashMap<>();
        /** 1. 根据挂号单ID 查询 病例信息*/
        HisCareHistory careHistory = careHistoryService.selectHisCareHistoryByRegId(regId);
        if (careHistory == null){
            return AjaxResult.error("请输入正确的ID查询");
        }
        /** res 存储返回数据*/
        res.put("careHistory",careHistory);
        res.put("careOrders",new ArrayList<>());
        /** 根据病例ID 查询处方信息*/
        List<HisCareOrder>  careOrders = orderService.selectHisCareOrderListByChId(careHistory.getChId());
        if (careOrders.isEmpty()){
            return AjaxResult.error("未找到病例信息");
        }
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (HisCareOrder careOrder : careOrders) {
            Map<String,Object> map = new HashMap<>();
            map.put("careOrderItems",new ArrayList<>());
            /** 查询未支付的订单详情 */
            List<HisCareOrderItem> careOrderItems = orderItemService.selectHisCareOrderItemListByCoIdAndStatus(careOrder.getCoId(), "0");
            if (careOrderItems.isEmpty()){
               continue;
            }else {
                /** 重新计算总价*/
                double allAmount = 0;
                for (HisCareOrderItem careOrderItem : careOrderItems) {
                    allAmount += careOrderItem.getAmount();
                }
                /** 封装 总价 类型 数据*/
                map.put("careOrderItems",careOrderItems);
                map.put("allAmount",allAmount);
                map.put("coType", careOrder.getCoType());
                map.put("coId", careOrder.getCoId());
                mapList.add(map);
            }
        }
        if (mapList.isEmpty()){
            return AjaxResult.error("错误");
        }else {
            res.put("careOrders",mapList);
            return AjaxResult.success(res);
        }
    }
}
